/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mapper;

import Bean.AccountBean;
import java.sql.*;
import java.util.*;

/**
 *
 * @author Quang Thao
 */
public class AccountMapper {
    DBMapper map;
    
    public AccountMapper() throws SQLException
    {
        map = new DBMapper();
    }
    
    public boolean IsTrue(Bean.AccountBean acc) throws Exception
    {
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Account Where ID='" + acc.getID() 
                + "' AND Pass='" + acc.getPass() + "'");
        if (rs.next())
            return true;
        return false;
    }
    
    public boolean IsExist(Bean.AccountBean acc) throws Exception
    {
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Account Where ID='" + acc.getID() 
                + "'");
        if (rs.next())
            return true;
        return false;
    }
    
    public Bean.AccountBean GetInfo(String id) throws Exception
    {
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Account Where ID='" + id + "'");
        Bean.AccountBean acc = new Bean.AccountBean();
        if (rs != null && rs.next())
        {
            acc.setID(rs.getString(1));
            acc.setPass(rs.getString(2));
            acc.setName(rs.getString(3));
            acc.setEmail(rs.getString(4));
            acc.setPhone(rs.getString(5));
            acc.setAddress(rs.getString(6));
            acc.setType(rs.getString(7));
        }
        return acc;
    }
    
    public List GetAll() throws Exception
    {
        List list = new ArrayList<Bean.AccountBean>();
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Account");
        if (rs != null)
        {
            while (rs.next())
            {
                Bean.AccountBean acc = new Bean.AccountBean();
                acc.setID(rs.getString(1));
                acc.setPass(rs.getString(2));
                acc.setName(rs.getString(3));
                acc.setEmail(rs.getString(4));
                acc.setPhone(rs.getString(5));
                acc.setAddress(rs.getString(6));
                acc.setType(rs.getString(7));
                list.add(acc);
            }
        }
        return list;
    }
    
    public List GetListByType(String type) throws Exception
    {
        List list = new ArrayList<Bean.AccountBean>();
        ResultSet rs = map.ExecuteQuery("SELECT * FROM Account WHERE Type='" + type + "'");
        if (rs != null)
        {
            while (rs.next())
            {
                Bean.AccountBean acc = new Bean.AccountBean();
                acc.setID(rs.getString(1));
                acc.setPass(rs.getString(2));
                acc.setName(rs.getString(3));
                acc.setEmail(rs.getString(4));
                acc.setPhone(rs.getString(5));
                acc.setAddress(rs.getString(6));
                acc.setType(rs.getString(7));
                list.add(acc);
            }
        }
        return list;
    }
    
    public int Add(Bean.AccountBean acc) throws Exception
    {
        return map.ExecuteNonQuery("Insert Into Account Values('"
            + acc.getID() + "','" + acc.getPass() + "','" + acc.getName() + "','"
            + acc.getEmail() + "','" + acc.getAddress() + "','" + acc.getPhone() + "','"
            + acc.getType() + "')");
    }

    public int Delete(AccountBean acc) throws Exception {
        return map.ExecuteNonQuery("Delete From Account Where Account_ID='" + acc.getID() + "'");
    }
}
